package com.java.business.system.permis.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.java.business.system.permis.bean.Menu;
import com.java.business.system.permis.dao.MenuMapper;
import com.java.core.bean.LowerCaseKeyMap;
import com.java.core.utils.IdUtils;

@Transactional
@Service
public class MenuService {

	@Autowired
	private MenuMapper mapper;

	public void add(Menu menu) throws RuntimeException {
		if (StringUtils.isBlank(menu.getParentid())) {
			menu.setParentid("0");
		}
		menu.setMenuid(IdUtils.uuid());
		mapper.add(menu);
	}

	public void update(Menu menu) throws RuntimeException {
		mapper.update(menu);
	}

	public void delete(Menu menu) throws RuntimeException {
		mapper.delete(menu.getMenuid());
	}

	public void setDisabled(Menu menu) throws RuntimeException {
		mapper.setDisabled(menu);
	}

	public void setReplace(String[] menuids, String parentid) {
		for (int i = 0; i < menuids.length; i++) {
			Menu menu = new Menu();
			menu.setMenuid(menuids[i]);
			menu.setParentid(parentid);
			menu.setPriority(i);
			mapper.setReplace(menu);
		}
	}

	public Menu getMenuById(String menuid) {
		return mapper.getMenuById(menuid);
	}

	public List<LowerCaseKeyMap> getMenuList(Menu menu) {
		if (StringUtils.isBlank(menu.getParentid())) {
			menu.setParentid("0");
		}
		List<LowerCaseKeyMap> list = mapper.getMenuList(menu);
		for (LowerCaseKeyMap map : list) {
			menu.setParentid((String) map.get("menuid"));
			List<LowerCaseKeyMap> children = getMenuList(menu);
			if ("0".equals(String.valueOf(map.get("isleaf")))) {
				map.put("leaf", false);
			} else {
				map.put("leaf", true);
			}
			if (children.size() > 0) {
				if ("1".equals(String.valueOf(map.get("disabled")))) {
					for (LowerCaseKeyMap child : children) {
						child.put("disabled", "1");
					}
				}
				map.put("leaf", false);
				map.put("children", children);
			}
			map.put("expanded", true);
		}
		return list;
	}

	public List<LowerCaseKeyMap> getMenuListByRole(String roleid,
			String parentid) {
		if (StringUtils.isBlank(parentid)) {
			parentid = "0";
		}
		List<LowerCaseKeyMap> list = mapper.getMenuListByRole(roleid, parentid);
		for (LowerCaseKeyMap map : list) {
			List<LowerCaseKeyMap> children = getMenuListByRole(roleid,
					String.valueOf(map.get("menuid")));
			if ("0".equals(String.valueOf(map.get("checked")))) {
				map.put("checked", false);
			} else {
				map.put("checked", true);
			}
			if (children.size() > 0) {
				if ("1".equals(String.valueOf(map.get("disabled")))) {
					for (LowerCaseKeyMap child : children) {
						child.put("disabled", "1");
					}
				}
				map.put("leaf", false);
				map.put("children", children);
			} else {
				map.put("leaf", true);
			}
			map.put("expanded", true);
		}
		return list;
	}

	public List<LowerCaseKeyMap> getMenuListByUser(String userid,
			String parentid) {
		if (StringUtils.isBlank(parentid)) {
			parentid = "0";
		}
		List<LowerCaseKeyMap> list = mapper.getMenuListByUser(userid, parentid);
		for (LowerCaseKeyMap map : list) {
			List<LowerCaseKeyMap> children = getMenuListByUser(userid,
					String.valueOf(map.get("menuid")));
			if (children.size() > 0) {
				if ("1".equals(String.valueOf(map.get("disabled")))) {
					for (LowerCaseKeyMap child : children) {
						child.put("disabled", "1");
					}
				}
				map.put("leaf", false);
				map.put("children", children);
			} else {
				map.put("leaf", true);
			}
			map.put("expanded", true);
		}
		return list;
	}
}
